//
//  WCIAlerterTableView.m
//  WCImApp
//
//  Created by alfie on 2019/12/5.
//  Copyright © 2019 WCImApp. All rights reserved.
//

#import "WCIAlerterTableView.h"
#import <Masonry/Masonry.h>

@interface WCIAlerterTableView () <UITableViewDelegate, UITableViewDataSource>

/** title */
@property (nonatomic, strong) UILabel                    *titleLb;

/** message */
@property (nonatomic, strong) UILabel                    *messageLb;

/** 数据 */
@property (strong, nonatomic) NSArray                    *titles;

@end


@implementation WCIAlerterTableView
static CGFloat cornerRadius = 16.f;  // 圆角

#pragma mark - 构造
- (instancetype)init {
    if (self = [super initWithFrame:(CGRectMake(0, 0, 0, 0)) style:(UITableViewStylePlain)]) {
        
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = YES;
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.delegate = self;
        self.dataSource = self;
        self.backgroundColor = [UIColor assist_AlertBG_Color];
        self.showsVerticalScrollIndicator = NO;
        if (@available(iOS 15, *)) {
            self.sectionHeaderTopPadding = 0;
        }

    }
    return self;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [UILabel new];
        _titleLb.textColor = [UIColor assist_Text_color];
        _titleLb.font = WCIFontRegular(14);
        _titleLb.numberOfLines = 0;
        _titleLb.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLb;
}


- (UIButton *)cancelBtn {
    if (!_cancelBtn) {
        _cancelBtn = [UIButton new];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn.titleLabel setFont:WCIFontRegular(16)];
        [_cancelBtn setTitleColor:[UIColor assist_AlertCancel_Color] forState:UIControlStateNormal];
        [_cancelBtn setBackgroundColor:[UIColor assist_AlertCancelBg_Color]];
        ViewRadius(_cancelBtn, 8);
    }
    return _cancelBtn;
}

#pragma mark - 绑定数据
- (void)attachTitles:(NSArray *)titles title:(NSString *)title message:(NSString *)message {
    self.titles = titles;
    if (title.length > 0) {
        if ([title isKindOfClass:NSString.class]) {
            self.titleLb.text = title;
        } else {
            self.titleLb.attributedText = title;
        }
    }else if (message.length > 0){
        self.titleLb.text = message;
    }

    [self reloadData];
}



#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self dequeueReusableCellWithIdentifier:@"MOAlerterTableViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"MOAlerterTableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor assist_AlertBG_Color];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.numberOfLines = 0;
    }
    if (self.showLeftImageIndexPath.section == indexPath.section && self.showLeftImageIndexPath.row == indexPath.row) {
        [cell.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.center.offset(0);
//            make.left.offset(15);
//            make.right.offset(-15);
        }];
        [cell.contentView addSubview:self.leftImageView];
        [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(self.leftImageView.frame.size);
            make.right.equalTo(cell.textLabel.mas_left).offset(-5);
            make.centerY.equalTo(cell.textLabel);
        }];
    }
    
    
    if (self.titles.count == 1) {
        if ([self.titles.firstObject isKindOfClass:NSString.class]) {
            if ([self.titles.firstObject mw_isEqualToString:@"删除联系人"] || [self.titles.firstObject mw_isEqualToString:@"确定"]|| [self.titles.firstObject mw_isEqualToString:@"移出黑名单"] || [self.titles.firstObject mw_isEqualToString:@"删除"]) {
                cell.textLabel.textColor = [UIColor main_Red_color];
                cell.textLabel.font = WCIFontMedium(16);
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
            }else{
                cell.textLabel.textColor = [UIColor main_Text_color];
                cell.textLabel.font = WCIFontMedium(16);
                cell.textLabel.textAlignment = NSTextAlignmentCenter;
            }
        }
    }else{
        cell.textLabel.textColor = [UIColor main_Text_color];
        cell.textLabel.font = WCIFontMedium(16);
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    NSString *title = self.titles[indexPath.row];
    if ([title isKindOfClass:NSString.class]) {
        cell.textLabel.text = title;
        if ([title mw_isEqualToString:@"语音通话"]) {
            UIImageView *img = [UIImageView new];
            [cell.contentView addSubview:img];
            img.image = [UIImage imageNamed:@"contacts_voice_call"];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(22, 22));
                make.right.equalTo(cell.textLabel.mas_left).offset(-10);
                make.centerY.equalTo(cell.textLabel);
            }];
            [cell.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(15);
                make.centerY.mas_equalTo(0);
            }];
        }else if ([title mw_isEqualToString:@"视频通话"]){
            UIImageView *img = [UIImageView new];
            [cell.contentView addSubview:img];
            img.image = [UIImage imageNamed:@"contacts_voice_video"];
            [img mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(22, 22));
                make.right.equalTo(cell.textLabel.mas_left).offset(-10);
                make.centerY.equalTo(cell.textLabel);
            }];
            [cell.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(15);
                make.centerY.mas_equalTo(0);
            }];
        }else{
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
        }
    } else {
        cell.textLabel.attributedText = title;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    cell.textLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString *title = self.titles[indexPath.row];
    if ([title isKindOfClass:NSAttributedString.class]) {
        title = [(NSAttributedString *)title string];
    }
    if (!title.length) {
        return;
    }
    
    if ([self.alerterDelegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {
        [self.alerterDelegate tableView:self didSelectRowAtIndexPath:indexPath];
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titles[indexPath.row];
    if ([title isKindOfClass:NSAttributedString.class]) {
        title = [(NSAttributedString *)title string];
    }
    return title.length ? 55 : 0.0001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
                
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor assist_AlertBG_Color];
        [view addSubview:self.titleLb];
        
        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(15);
            make.right.offset(-15);
            make.top.offset(26);
        }];
        
        [view layoutSubviews];
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        return [self heightForHeader];
    }
    return 0.001;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)heightForHeader {
    
    if (self.titleLb.text.length <= 0 ) {
        return 20;
    }
    
    UIFont *messageFont;
    CGFloat h = 41;

    CGSize maxSize = CGSizeMake((SCREEN_WIDTH - 30), MAXFLOAT);
    CGFloat titleH = [self.titleLb.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : WCIFontRegular(14)} context:nil].size.height;
    
  
    return h + titleH ;
}

@end
